package com.performanceManagement.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * excel表格生成方法，依赖于easy-poi。
 * 需求时使用。
 * PerformanceManagement的service层提供员工绩效表格的生成方法。
 */


public class ExcelUtils {

    /**
     * 传入最详细的数据，包括：标题、工作表名，pojo的class对象，数据集合，文件路径和表格名。
     * @param title
     * @param sheetName
     * @param pojoClass
     * @param dataSet
     * @param fileName
     * @param filePath
     * @throws IOException
     */

    public void excelExport(String title,String sheetName, Class<?> pojoClass, Collection<?> dataSet,String filePath,String fileName) throws IOException {
        ExportParams params = new ExportParams(title,sheetName);
        Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, dataSet);
        FileOutputStream fos = new FileOutputStream(filePath+fileName+".xls");
        workbook.write(fos);
        fos.close();
    }

    /**
     * 传入参数为：pojo的class对象，数据集合，文件路径和表格名。
     * 标题名默认为空，工作区间为sheet1
     * @param pojoClass
     * @param dataSet
     * @param fileName
     * @param filePath
     * @throws IOException
     */

    public void excelExport(Class<?> pojoClass, Collection<?> dataSet,String filePath,String fileName) throws IOException {
        ExportParams params = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, dataSet);
        FileOutputStream fos = new FileOutputStream(filePath+fileName+".xls");
        workbook.write(fos);
        fos.close();

    }

    /**
     * 仅传入pojo和数据集合和文件名
     * 路径为工程下
     * @param pojoClass
     * @param dataSet
     * @param fileName
     * @throws IOException
     */

    public void excelExport(Class<?> pojoClass, Collection<?> dataSet,String fileName) throws IOException {
        ExportParams params = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, dataSet);
        FileOutputStream fos = new FileOutputStream(fileName+".xls");
        workbook.write(fos);
        fos.close();
    }

    /**
     * 仅传入pojo和数据集合。
     * 标题名默认为空，工作区间为sheet1
     * 路径为工程下，文件名为生成日期。
     * @param pojoClass
     * @param dataSet
     * @throws IOException
     */
    public void excelExport(Class<?> pojoClass, Collection<?> dataSet) throws IOException {
        ExportParams params = new ExportParams();
        Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, dataSet);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
        String str = sdf.format(date);
        FileOutputStream fos = new FileOutputStream(str+".xls");
        workbook.write(fos);
        fos.close();
    }

}
